package com.timmy.chi.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.timmy.chi.R;
import com.timmy.chi.adapter.OrderAdapter;
import com.timmy.chi.model.beans.UserOrder;
import com.timmy.chi.ui.activity.FoodsActivity;
import com.timmy.chi.utils.Constants;
import com.timmy.chi.utils.network.MyHttp;
import com.timmy.chi.utils.network.ResultCallback;

import java.util.List;


/**
 * 订单页面
 */

public class OrderFragment extends Fragment implements AdapterView.OnItemClickListener {

    private static String TAG = OrderFragment.class.getSimpleName();
    ListView mOrderListview;
    SwipeRefreshLayout mSrlOrder;
    private View mView;
    private Gson mGson;
    private OrderAdapter mOrderAdapter;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        if (mView == null) {
            mView = View.inflate(getContext(), R.layout.fragment_order, null);
            mGson = new Gson();
            initView();
            initData();
        }

        return mView;
    }

    private void initView() {
        mOrderListview = (ListView) mView.findViewById(R.id.order_listview);
        mSrlOrder = (SwipeRefreshLayout) mView.findViewById(R.id.srl_order);

        mOrderAdapter = new OrderAdapter(getActivity());
        mOrderListview.setAdapter(mOrderAdapter);
        mSrlOrder.setOnRefreshListener(mOnRefreshListener);
        mOrderListview.setOnItemClickListener(this);
    }

    private void initData() {
        MyHttp.getOrder(new ResultCallback() {
            @Override
            public void onSuccess(String s) {
                mSrlOrder.setRefreshing(false);

                UserOrder userOrder = mGson.fromJson(s, UserOrder.class);
                if (userOrder.getResponseState() == 0) {

                    List<UserOrder.OrdersBean> orders = userOrder.getOrders();
                    mOrderAdapter.setList(orders);
                }
            }

            @Override
            public void onError() {
                mSrlOrder.setRefreshing(false);
                Toast.makeText(getActivity(), "请检查网络", Toast.LENGTH_SHORT).show();
            }
        });

    }

    private SwipeRefreshLayout.OnRefreshListener mOnRefreshListener = new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            initData();
        }
    };

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        UserOrder.OrdersBean item = mOrderAdapter.getItem(position);
        int id1 = item.getOrder().getId();

        // 跳转页面
        Intent intent = new Intent(getActivity(), FoodsActivity.class);
        intent.putExtra(Constants.ORDERID, id1);
        startActivity(intent);
    }
}
